enyo.kind({
    name: "MyApps.Partners",
    kind: enyo.VFlexBox,
    events: {
        onBack: "",
    },
    components: [    /**
     * service
     */
    {
        name: "getData",
        kind: "WebService",
        url: "http://query.yahooapis.com/v1/public/yql?q=select * from xml where url= 'http://golfplus.mobile2you.fr/golf_pros.xml' &format=json&callback=",
        onSuccess: "gotData",
        onFailure: "gotDataFailure"
    },  {
        name: "pane",
        kind: "Pane",
        flex: 1,
        transitionKind: "enyo.transitions.LeftRightFlyin",
        
        
        components: [{
            kind: "VFlexBox",
            components: [{
                kind: "Scroller",
                flex: 1,
                className: "list",
                components: [{
                    name: "list",
                    kind: "VirtualRepeater",
                    flex: 1,
                    onSetupRow: "listSetupRow",
                    components: [{
                        kind: "Item",
                        layoutKind: "VFlexLayout",
                        components: [{
                            name: "title",
                            kind: "Divider"
                        }, {
                            kind: "HFlexBox",
                            components: [{
                                name: "photo",
                                kind: "Image",
                                className: "photo",
                            }, {
                                kind: "VFlexBox",
                                components: [{
                                    name: "name",
                                    className: "partnername",
                                }, {
                                    name: "phone",
                                    className: "partnertel",
                                }, ]
                            }, {
                                name: "logo",
                                kind: "Image",
                            }]
                        }, ],
                        onclick: "listItemClick"
                    }]
                }, ]
            }, ]
        }, ]
    
    }, ],
    
    
    create: function(){
        this.inherited(arguments);
        this.data = [];
        this.$.getData.call();
    },
    
    getGroupName: function(inIndex){
        var r0;
        if (inIndex != 0) {
            r0 = this.data[inIndex - 1].golf.nom;
        }
        var r1 = this.data[inIndex].golf.nom;
        
        //if previous partner in list belongs to different club, return this club name
        return r0 != r1 ? r1 : null;
    },
    
    // order partners by golfclub, surname,
    orderByGolfClub: function(obj1, obj2){
        //by golfclub
        var nameA = obj1.golf.nom.toLowerCase();
        var nameB = obj2.golf.nom.toLowerCase();
        if (nameA < nameB) 
            return -1;
        if (nameA > nameB) 
            return 1;
        
        //by surname
        nameA = obj1.nom.toLowerCase();
        nameB = obj2.nom.toLowerCase();
        if (nameA < nameB) 
            return -1;
        if (nameA > nameB) 
            return 1;
        
        return 0;
    },
    
    gotData: function(inSender, inResponse){
        this.data = inResponse.query.results['golf-pros']['golf-pro'];
        this.data.sort(this.orderByGolfClub);
        this.$.pane.render();
        
        console.log("got Data ");
    },
    gotDataFailure: function(inSender, inResponse){
        console.log("got failure from getData");
    },
    listSetupRow: function(inSender, inRow){
        var f = this.data[inRow];
        if (f) {
            var groupName = this.getGroupName(inRow);
            this.$.title.setCaption(groupName);
            this.$.title.canGenerate = Boolean(groupName);
            this.$.photo.setSrc("http://golfplus.mobile2you.fr/images/" + f.thumb)
            this.$.name.setContent(f.prenom + " " + f.nom);
            this.$.phone.setContent(f.tel);
            return true;
        }
    },

});

